package com.hucc.api.service;

import com.hucc.api.domain.entity.Group;
import com.hucc.api.domain.entity.Project;
import com.hucc.api.domain.enums.GroupTypeEnum;
import com.hucc.api.domain.exceptions.BusinessException;
import com.hucc.api.domain.req.ProjectReq;
import com.hucc.api.domain.res.ProjectRes;
import com.hucc.api.repository.GroupMapper;
import com.hucc.api.repository.ProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author huguocai
 */
@Slf4j
@Service
public class ProjectService {
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    GroupMapper groupMapper;

    public void add(ProjectReq projectReq) throws BusinessException {
        if (projectReq == null) {
            throw new BusinessException("参数不能为空");
        }

        if (StringUtils.isEmpty(projectReq.getName())) {
            throw new BusinessException("项目名不能为空");
        }

        if (projectReq.getName().length() > 100) {
            throw new BusinessException("项目名长度不能超过100");
        }

        if (projectReq.getDesc() != null && projectReq.getDesc().length() > 500){
            throw new BusinessException("项目描述长度不能超过500");
        }

        List<Project> projects = projectMapper.getByName(projectReq.getName());
        if (!projects.isEmpty()) {
            throw new BusinessException("项目名已存在");
        }

        Project project = new Project();
        project.setName(projectReq.getName());
        project.setDesc(projectReq.getDesc());
        project.setCreatetime(new Date());
        project.setUpdatetime(new Date());

        try {
            projectMapper.insertSelective(project);

            Group group = new Group();
            group.setProjectId(project.getId());
            group.setName("default");
            group.setType(GroupTypeEnum.DEFAULT.getType());
            group.setCreatetime(new Date());
            group.setUpdatetime(new Date());
            groupMapper.insertSelective(group);
        } catch (Exception e) {
            log.error("add project error, projectReq:{}", projectReq, e);
            throw new BusinessException("添加项目信息到数据库失败", e);
        }
    }

    public List<ProjectRes> list() throws BusinessException {
        List<ProjectRes> projectResList = new ArrayList<>();
        try {
            List<Project> all = projectMapper.getAll();
            for (Project project : all) {
                projectResList.add(ProjectRes.build(project));
            }
            return projectResList;
        } catch (Exception e) {
            log.error("list project error", e);
            throw new BusinessException("获取项目列表失败", e);
        }
    }

    public Project getByName(String projectName) {
        List<Project> projects = projectMapper.getByName(projectName);
        if (projects.isEmpty()) {
            return null;
        }
        return projects.get(0);
    }
}
